package fun.boomcat.luckyhe.mirai.plugin.botcontrol;

import fun.boomcat.luckyhe.mirai.plugin.botcontrol.command.ControlCommand;
import fun.boomcat.luckyhe.mirai.plugin.botcontrol.listener.EventListener;
import fun.boomcat.luckyhe.mirai.plugin.botcontrol.util.BotUtil;
import fun.boomcat.luckyhe.mirai.plugin.botcontrol.util.ConfigUtil;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.extension.PluginComponentStorage;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.console.permission.PermissionId;
import net.mamoe.mirai.console.permission.PermissionRegistryConflictException;
import net.mamoe.mirai.console.permission.PermissionService;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import net.mamoe.mirai.event.GlobalEventChannel;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class BotControl extends JavaPlugin {
    public static final BotControl INSTANCE = new BotControl();

    private Permission controlPerm;

    private BotControl() {
        super(new JvmPluginDescriptionBuilder(
                "luckyhe.botcontrol",
                "1.0"
        ).build());
    }

    @Override
    public void onEnable() {
        INSTANCE.getLogger().info("请注意：该插件只支持单个bot，如果有多个bot将取第一个bot使用");
        INSTANCE.getLogger().info("开始加载插件");
        loadPermissions();
        INSTANCE.getLogger().info("注册权限完成");
        loadCommands();
        INSTANCE.getLogger().info("注册指令完成");
        loadListeners();
        INSTANCE.getLogger().info("注册监听器完成");
    }

    @Override
    public void onLoad(@NotNull PluginComponentStorage $this$onLoad) {
        try {
            ConfigUtil.init(getConfigFolder(), getResource("config.yml", StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadPermissions() {
        PermissionId controlPermId = new PermissionId("botcontrol", "command.control");

        try {
            controlPerm = PermissionService.getInstance().register(
                    controlPermId,
                    "控制bot相关指令",
                    Permission.getRootPermission()
            );
        } catch (PermissionRegistryConflictException e) {
            e.printStackTrace();
        }
    }

    private void loadCommands() {
        String commandPrefix = CommandManager.INSTANCE.getCommandPrefix();

        String[] controlSecondaryNames = {"控制", "ctl", "ctrl", "c", "bot"};
        CommandManager.INSTANCE.registerCommand(new ControlCommand(
                this,
                "control",
                controlSecondaryNames,
                commandPrefix + "control <指令>",
                "用于操作bot",
                controlPerm,
                false
        ), false);
    }

    private void loadListeners() {
        GlobalEventChannel.INSTANCE.registerListenerHost(new EventListener());
    }
}
